package com.kingtone.rongji.fragment;

import java.util.ArrayList;
import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.kingtone.rongji.R;
import com.kingtone.rongji.adapter.MyCouponAdapter;
import com.kingtone.rongji.biz.PromotionCode;
import com.kingtone.rongji.tool.AppContasts;
import com.kingtone.rongji.tool.RongJiRestClient;
import com.kingtone.rongji.tool.RongJiUtils;
import com.kingtone.rongji.tool.SharedPreferencesUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class MyCouponFragment extends BaseFragment {

	private TextView title = null;
	private ImageView backBtn = null;
	private ImageView leftIcon = null;
	private TextView rightTitle = null;
	private ListView couponList = null;
	private Activity mActivity;
	private String notUseAmount = "0";
	private String inUseAmount = "0";
	private String invalidAmount = "0";
	private RadioButton notUseBtn;
	private RadioButton inUseBtn;
	private RadioButton invalidBtn;
	private MyCouponAdapter couponAdapter;
	private RadioGroup group = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		mActivity =  this.getActivity();
		super.onCreate(savedInstanceState);
		getPromotionList(0);
	}
	
	public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle) {
		View localView = paramLayoutInflater.inflate(R.layout.fragment_my_coupon, paramViewGroup, false);
		setupView(localView);
		initData();
		return localView;
	}
	
	private void setupView(View paramView)
	{
		title = (TextView)paramView.findViewById(R.id.title);
		title.setText(getString(R.string.my_copuon));
		backBtn = (ImageView)paramView.findViewById(R.id.back);
		backBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{
				getActivity().onBackPressed();
			}
		});
		
		leftIcon = (ImageView)paramView.findViewById(R.id.left_icon);
		leftIcon.setBackgroundResource(R.drawable.icon_hui);
		
		rightTitle = (TextView)paramView.findViewById(R.id.right_title);
		rightTitle.setText(getString(R.string.my_copuon));
		
		couponList = (ListView)paramView.findViewById(R.id.my_coupon_list);
		notUseBtn = (RadioButton)paramView.findViewById(R.id.radio_not_use);
		inUseBtn = (RadioButton)paramView.findViewById(R.id.radio_in_use);
		invalidBtn = (RadioButton)paramView.findViewById(R.id.radio_invalid);
		group = (RadioGroup)paramView.findViewById(R.id.coupon_radio_group);
		group.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId)
			{
				if(checkedId == R.id.radio_not_use)
				{
					getPromotionList(0);
				}else if(checkedId == R.id.radio_in_use)
				{
					getPromotionList(1);
				}else if(checkedId == R.id.radio_invalid)
				{
					getPromotionList(2);
				}
			}
		});
	}
	
	private void initData()
	{
		
	}
	
	public void getPromotionList(int status)
	{
		String userId = SharedPreferencesUtil.getInstance(mActivity).getString(AppContasts.USERID_KEY);
		RongJiRestClient.getPromotionList(userId, 0, 1000, status, new AsyncHttpResponseHandler() {

			@Override
			public void onStart() {
				RongJiUtils.showEmptyProgress(getActivity());
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
			    if(arg2 != null)
			    {
			    	String filureStr = new String(arg2);
					Log.d("failure", filureStr);
			    }
				RongJiUtils.dismissProgress();
			}

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				
				RongJiUtils.dismissProgress();
				
				if(arg2 != null && !new String(arg2).equalsIgnoreCase("null"))
				{
					String result = new String(arg2);
					Log.d("result", result);
					JSONObject obj = null;
					ArrayList<PromotionCode> list = new ArrayList<PromotionCode>();
					try {
							obj = new JSONObject(result);
							if(obj.has("Data"))
							{
								JSONArray pArray = obj.getJSONArray("Data");
								Gson gson = new Gson();
								JsonParser parser = new JsonParser();
								JsonElement element = parser.parse(pArray.toString());
								if(element instanceof JsonArray)
								{
									JsonArray array = element.getAsJsonArray();
									for(JsonElement myCoupon:array) {
										PromotionCode object = gson.fromJson(myCoupon, PromotionCode.class);
										list.add(object);
									}
								}
								couponAdapter = new MyCouponAdapter(mActivity, list);
								couponList.setAdapter(couponAdapter);
							}
							if(obj.has("Tag"))
							{
								String tags = obj.getString("Tag");
								if(!TextUtils.isEmpty(tags))
								{
									String[] amounts = tags.split(",");
									if(amounts!= null && amounts.length == 3)
									{
										notUseAmount = amounts[0];
										inUseAmount = amounts[1];
										invalidAmount = amounts[2];
									}
								}
							}
							notUseBtn.setText(mActivity.getString(R.string.coupon_not_use, new Object[]{notUseAmount}));
							inUseBtn.setText(mActivity.getString(R.string.coupon_in_use, new Object[]{inUseAmount}));
							invalidBtn.setText(mActivity.getString(R.string.coupon_invalid, new Object[]{invalidAmount}));
					} catch (JSONException e) {

						e.printStackTrace();
					}
				}
			}	
		});
	}
}
